package com.wwwarehouse.usercenter.fragment.businessunit;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;

import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.custom_widget.CustomViewPagerInternal;
import com.wwwarehouse.common.custom_widget.SlideBarView;
import com.wwwarehouse.common.custom_widget.state_layout.StateLayout;
import com.wwwarehouse.common.tripartite_widget.viewindicator.CirclePageIndicator;
import com.wwwarehouse.usercenter.R;
import com.wwwarehouse.usercenter.adapter.UserFragmentPagerAdapter;
import com.wwwarehouse.usercenter.bean.BusinessInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * Creator by zhenwang.xiang on 2017/9/26 15:10
 * Description:Gridview点击item进入创建业务单位viewpager
 */

public class BusinessUnitViewPagerFragment extends BaseFragment {

    private StateLayout mStateLayout;
    private CustomViewPagerInternal mCustomViewPagerInternal;
    private CirclePageIndicator mCirclePageIndicator;
    private SlideBarView mSlideBarView;
    private List<Fragment> mFragmentList;
    int mPosition;
    private String mTitle;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_business_unit_view_pager, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();

        if (getArguments() != null) {
            mPosition = getArguments().getInt("position");
            mTitle=getArguments().getString("title");
            final List<BusinessInfo> list = (List<BusinessInfo>) getArguments().getSerializable("griddata");
            if (list != null) {
                mStateLayout.showContentView();
                for (int i = 0; i < list.size(); i++) {
                    BusinessUnitInfoFragment businessUnitInfoFragment = new BusinessUnitInfoFragment();
                    Bundle bundle = new Bundle();
                    bundle.putInt("businessUnitId", list.get(i).getBuId());
                    bundle.putInt("flag",1);
                    bundle.putString("title", mTitle);
                    businessUnitInfoFragment.setArguments(bundle);
                    mFragmentList.add(businessUnitInfoFragment);
                }
                mCustomViewPagerInternal.setAdapter(new UserFragmentPagerAdapter(getChildFragmentManager(), mFragmentList));
                setData(list.size());

            } else {
                mStateLayout.showEmptyView(true);
            }
        }

    }

    public void setData(int page) {
        mCustomViewPagerInternal.setCurrentItem(mPosition);
        if (page <= 6) {
            if (page > 1) {
                mCirclePageIndicator.setVisibility(View.VISIBLE);
                mSlideBarView.setVisibility(View.GONE);
                mCirclePageIndicator.setViewPager(mCustomViewPagerInternal, mPosition);
            } else {
                mCirclePageIndicator.setVisibility(View.GONE);
                mSlideBarView.setVisibility(View.GONE);
            }
        } else {
            mCirclePageIndicator.setVisibility(View.GONE);
            mSlideBarView.setVisibility(View.VISIBLE);
            mSlideBarView.setTotalPage(page);
            mSlideBarView.setOnSlideChange(new SlideBarView.OnSlideChange() {
                @Override
                public void onSlideChange(int page) {
                    mCustomViewPagerInternal.setCurrentItem(page, false);
                }
            });
            mSlideBarView.setViewPagerInternal(mCustomViewPagerInternal);
            ViewTreeObserver observer = mSlideBarView.getViewTreeObserver();
            observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    mSlideBarView.setCurrPage(mPosition);
                    mSlideBarView.setSlideBlockText("" + (mPosition + 1));
                    mSlideBarView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                }
            });

        }
    }

    private void initView() {
        mStateLayout = $(R.id.user_state_layout);
        mCustomViewPagerInternal = $(R.id.user_viewpager);
        mCirclePageIndicator = $(R.id.user_circlepage);
        mSlideBarView = $(R.id.user_slidebar);

        mStateLayout.showProgressView(true);
        mFragmentList = new ArrayList<>();

    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof BusinessUnitViewPagerFragment) {
            mActivity.setTitle(mTitle);
        }
    }

    @Override
    public void requestDatas() {

    }

}
